package com.jz.yanshe.ui;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.jz.yanshe.base.BaseFragment;
import com.jz.yanshe.util.PreferencesHelper;

import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.MessageNotifyResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.R;

/**
 * Created by Administrator on 2018/3/16.
 */

public class MessageFragment extends BaseFragment implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.swipe_target)
    RecyclerView recyclerView;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;
    MyAdapter myAdapter = new MyAdapter();
    List<MessageWrapper> list = new ArrayList<>();

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initView() {
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 1;
            }
        });
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
        swipeToLoadLayout.setLoadMoreEnabled(false);

    }

    @Override
    protected void initData() {
        getMessage();

    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onRefresh() {
        getMessage();
    }


    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == 1) {
                return new HeadHodler(getLayoutInflater().inflate(R.layout.item_date, parent, false));
            } else if(viewType == 2){
                return new BodyHodler(getLayoutInflater().inflate(R.layout.item_message_notify, parent, false));
            }else{
                return new FootBody(getLayoutInflater().inflate(R.layout.item_decoration, parent, false));
            }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof HeadHodler) {
                ((HeadHodler) holder).tv_date.setText(list.get(position).date);
            }
            if (holder instanceof BodyHodler) {
                ((BodyHodler) holder).tv_title .setText(list.get(position).title);
                ((BodyHodler) holder).tv_time .setText(list.get(position).time);
                ((BodyHodler) holder).tv_notify1 .setText(list.get(position).notify1);
                ((BodyHodler) holder).tv_notify2 .setText(list.get(position).notify2);
            }

        }

        @Override
        public int getItemViewType(int position) {
            return list.get(position).type;
        }

        @Override
        public int getItemCount() {
            return list.size();
        }


        class HeadHodler extends RecyclerView.ViewHolder {
            TextView tv_date;

            public HeadHodler(View itemView) {
                super(itemView);
                tv_date = itemView.findViewById(R.id.tv_date);
            }
        }

        class BodyHodler extends RecyclerView.ViewHolder {
            TextView tv_notify1, tv_title, tv_notify2, tv_time;

            public BodyHodler(View itemView) {
                super(itemView);
                tv_notify1 = itemView.findViewById(R.id.tv_notify1);
                tv_title = itemView.findViewById(R.id.tv_title);
                tv_notify2 = itemView.findViewById(R.id.tv_notify2);
                tv_time = itemView.findViewById(R.id.tv_time);
            }
        }
        class FootBody extends RecyclerView.ViewHolder {

            public FootBody(View itemView) {
                super(itemView);
            }
        }
    }


    class MessageWrapper {
        public int type;
        public String date;
        public String time;
        public String title;
        public String notify1;
        public String notify2;
    }

    private void getMessage() {
        RestProxy.getInstance().getMessageNotify(PreferencesHelper.getInstance().getString(getActivity(), "token"), "4", new Observer<MessageNotifyResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(MessageNotifyResponse messageNotifyResponse) {
                list.clear();
                String date = "";
                for (int i = 0; i < messageNotifyResponse.listM.size(); i++) {
                    if (!date.equals(messageNotifyResponse.listM.get(i).add_time.split(" ")[0])) {
                        date = messageNotifyResponse.listM.get(i).add_time.split(" ")[0];
                        MessageWrapper wrapper = new MessageWrapper();
                        wrapper.date = messageNotifyResponse.listM.get(i).add_time.split(" ")[0];
                        wrapper.type = 1;
                        list.add(wrapper);
                    }
                    if (date.equals(messageNotifyResponse.listM.get(i).add_time.split(" ")[0])) {
                        MessageWrapper wrapper = new MessageWrapper();
                        wrapper.time = messageNotifyResponse.listM.get(i).add_time.split(" ")[1];
                        wrapper.title = messageNotifyResponse.listM.get(i).msg_title;
                        wrapper.notify1 = messageNotifyResponse.listM.get(i).msg_black_content;
                        wrapper.notify2 = messageNotifyResponse.listM.get(i).msg_red_content;
                        wrapper.type = 2;
                        list.add(wrapper);
                        MessageWrapper wrapper1 = new MessageWrapper();
                        wrapper1.type =3;
                        list.add(wrapper1);
                    }
                }
                myAdapter.notifyDataSetChanged();

            }

            @Override
            public void onError(Throwable e) {
                swipeToLoadLayout.setRefreshing(false);
            }

            @Override
            public void onComplete() {
                swipeToLoadLayout.setRefreshing(false);
            }
        });
    }
}
